$(function () {
  // 事件绑定 查找按钮
  $('#btnQuery').click(function () {
    let keyword = $('[name=abc]').val()
    let obj = {
      keyword: keyword,
    }
    console.log(obj)
    // 通过jQuery的AJAX向后端请求数据
    $.post('/test/query', obj, function (res) {
      console.log(res)
      let tb = $('.tb')
      let tbRow = $('.tbRow')
      tbRow.html('')

      res.forEach((item) => {
        let html = `
                <tr class="tbRow">
                    <td>${item.id}</td>
                    <td>${item.studentName}</td>
                    <td>
                        <input type="button" value="编辑" onclick="btnEdit(${item.id})">
                        <input type="button" value="删除" onclick="btnDel(${item.id})">
                    </td>
                </tr>
                `
        tb.append(html)
      })
    })
  })
  // 事件绑定 新增按钮
  $('#btnAdd').click(function () {
    console.log('你点新增')
    window.location.href = '/test/createOrEdit'
  })

  $('#btnSave').click(function () {
    let id = $('[name=id]').val()
    let name = $('[name=name]').val()

    let obj = {
      id,
      name,
    }

    $.post('/test/save', obj, function (res) {
      console.log(res)
      window.location.href = '/test/index'
    })
  })
  $('#btnCancel').click(function () {
    window.location.href = '/test/index'
  })
})

// 绑定编辑按钮，传递id值
function btnEdit(id) {
  console.log(`你点编辑按钮，id为${id}`)
  window.location.href = `/test/createOrEdit/${id}`
}
// 绑定删除按钮，传递id值
function btnDel(id) {
  console.log(`你点删除按钮，id为${id}`)
  $('#demo').text('kjasdjfsladkjf')
}
